package com.qf.taobei.service.impl;

import com.qf.taobei.entity.CartProduct;
import com.qf.taobei.entity.Product;
import com.qf.taobei.entity.RespBean;
import com.qf.taobei.entity.User;
import com.qf.taobei.entity.bo.CartAdd;
import com.qf.taobei.entity.bo.CartUser;
import com.qf.taobei.entity.bo.ProductCart;
import com.qf.taobei.mapper.CartMapper;
import com.qf.taobei.mapper.UserMapper;
import com.qf.taobei.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public RespBean addCartProduct(HttpServletRequest request, int productId, int number) {
        User user = (User) request.getSession().getAttribute("user");
        Integer integer =cartMapper .addCartProduct(user.getId(), productId, number);
        if (integer == 0){
            return RespBean.respError("加入购物车失败");
        }
        return RespBean.respSuccess("添加成功");
    }

    @Override
    public RespBean getProductList(HttpServletRequest request) {
        cartMapper.updateMoney();
        User user = (User) request.getSession().getAttribute("user");
        List<ProductCart> productList = cartMapper.getProductList(user.getId(),null);
        if (productList.size() == 0){
            return RespBean.respError("查询失败");
        }
        return RespBean.respSuccess("查询成功",productList);
    }

    @Override
    public RespBean deleteProductById(int productId) {
        Integer integer = cartMapper.deleteProductById(productId);
        if (integer == 0){
            return RespBean.respError("删除失败");
        }
        return RespBean.respSuccess("删除成功");
    }

    @Override
    public RespBean addProduct(int productId) {
        CartProduct cartProduct = cartMapper.getCartProductById(productId);
        Product product = cartMapper.getProductById(cartProduct.getPId());
        if (product.getRepertory() <= cartProduct.getNumber()){
            return RespBean.respError("库存不足");
        }
        Integer integer = cartMapper.addProduct(productId);
        CartProduct cartProduct2 = cartMapper.getCartProductById(productId);
        if (integer == 0){
            return RespBean.respError("添加失败");
        }
        return RespBean.respSuccess("添加成功",cartProduct2);
    }

    @Override
    public RespBean deleteProduct(int productId) {
        Integer integer = cartMapper.deleteProduct(productId);
        if (integer == 0){
            return RespBean.respError("减少失败");
        }
        CartProduct cartProduct = cartMapper.getCartProductById(productId);
        return RespBean.respSuccess("减少成功",cartProduct);
    }

    @Override
    public RespBean getProductListByName(HttpServletRequest request, String pName) {
        User user = (User) request.getSession().getAttribute("user");
        String name = "%"+pName+"%";
        List<ProductCart> productList = cartMapper.getProductList(user.getId(),name);
        if (productList.size() == 0){
            return RespBean.respError("您查询的商品不存在");
        }
        return RespBean.respSuccess("查询成功",productList);
    }

    @Override
    public RespBean deleteProductAll(List<Integer> list) {
        Integer integer = cartMapper.deleteProductAll(list);
        if (integer == 0){
            return RespBean.respError("删除失败");
        }
        return RespBean.respSuccess("删除成功");
    }

    @Override
    public RespBean getMoneyAll(List<Integer> list) {
        Integer moneyAll = cartMapper.getMoneyAll(list);
        if (moneyAll == 0){
            return RespBean.respError("查询失败");
        }
        return RespBean.respSuccess("查询成功",moneyAll);
    }

    @Override
    public RespBean getCartSum(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null){
            return RespBean.respError("请先登录");
        }
        Integer cartSum = cartMapper.getCartSum(user.getId());
        User user1 = userMapper.getUserById(user.getId());
        CartUser cartUser = new CartUser();
        cartUser.setNums(cartSum);
        cartUser.setUserName(user1.getUsername());
        return RespBean.respSuccess("查询成功",cartUser);
    }

    @Override
    public RespBean getProductID(HttpServletRequest request,List<Integer> list) {
        if(list .size() ==0){
            return RespBean.respSuccess("请选择商品");
        }
        List<CartAdd> cartAdds = cartMapper.getProductID(list);
        if (cartAdds.size() ==0){
            return RespBean.respError("结算错误");
        }
        request.getSession().setAttribute("cartProduct",cartAdds);
        return RespBean.respSuccess("结算成功",cartAdds);
    }
}
